package com.dstation.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.dao.CoinOperatedInfoMapper;
import com.dstation.dao.VideoInfoMapper;
import com.dstation.domain.*;
import com.dstation.service.interf.IVideoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements IVideoService {
    @Resource
    private VideoInfoMapper videoInfoMapper;
    @Resource
    private CoinOperatedInfoMapper coinOperatedInfoMapper;

    @Override
    public List<VideoInfo> findAllVideoInfoByExamineStatusAccess() {
        return videoInfoMapper.selectAllVideoInfoByExamineStatusAccess();
    }

    @Override
    public boolean addVideoInfo(VideoInfo videoInfo) {
        return videoInfoMapper.insertVideoInfo(videoInfo);
    }

    @Override
    public boolean addVideoTagInfo(ArrayList<TagInfo> labelInfoList, Long videoId, Timestamp timestamp) {
        TagInfo tagInfo = new TagInfo();
        boolean res = true;
        for(int i = 0; i < labelInfoList.size(); i++)  {
            tagInfo = labelInfoList.get(i);
            res = videoInfoMapper.insertVideoTagInfo(tagInfo, videoId, timestamp);
            if(!res) {
                break;
            }
        }
        return res;
    }

    @Override
    public IPage<VideoInfo> findMyVideo(Page ipage , Long userId) {
        return videoInfoMapper.selectMyVideo(ipage, userId);
    }

    @Override
    public String findVideoCoverageById(long videoId) {
        return videoInfoMapper.selectVideoCoverageById(videoId);
    }

    @Override
    public String findVideoNameById(long videoId) {
        return videoInfoMapper.selectVideoNameById(videoId);
    }

    @Override
    public boolean dropVideoInfoById(long videoId) {
        if(videoInfoMapper.deleteVideoInfoById(videoId) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public VideoInfo findVideoInfoById(long videoId) {
        return videoInfoMapper.selectVideoInfoById(videoId);
    }

    @Override
    public ClassificationInfo findClassificationById(Long videoClassId) {
        return videoInfoMapper.selectClassificationById(videoClassId);
    }

    @Override
    public List<TagInfo> findTagInfoByVideoId(long videoId) {
        return videoInfoMapper.selectTagInfoByVideoId(videoId);
    }

    @Override
    public boolean alterVideoInfo(VideoInfo videoInfo) {
        return videoInfoMapper.updateVideoInfo(videoInfo);
    }

    @Override
    public boolean dropTagInfoByVideoId(Long videoId) {
        return videoInfoMapper.deleteTagInfoByVideoId(videoId);
    }

    @Override
    public VideoInfo findVideoDetailInfoById(int videoId) {
        VideoInfo videoInfo = videoInfoMapper.findVideoDetailInfoById(videoId);
        videoInfo.setLikedNum(videoInfoMapper.findLikedNumByVideoId(videoId));
        return videoInfo;
    }

    @Override
    public VideoLikedInfo findUserIsPraiseVideoByVideoIdAndUserId(int videoId, Long userId) {
        return videoInfoMapper.selectUserIsPraiseVideoByVideoIdAndUserId(videoId, userId);
    }

    @Override
    public FollowTableInfo findUserIsNoticeUp(Long followUserId, Long userId) {
        return videoInfoMapper.findUserIsNoticeUp(followUserId, userId);
    }

    @Override
    public List<VideoCommentInfo> findCommentInfoListByVideoId(int videoId) {
        return videoInfoMapper.selectCommentInfoListByVideoId(videoId);
    }

    @Override
    public CollectionInfo findUserIsCollectVideoByUserIDAndVideoID(Long userId, int videoId) {
        return videoInfoMapper.selectUserIsCollectVideoByUserIDAndVideoID(userId, videoId);
    }

    @Override
    public boolean addHistoryInfo(HistoryInfo historyInfo) {
        return videoInfoMapper.insertHistoryInfo(historyInfo);
    }

    @Override
    public boolean alterVideoViewNumByVideoId(int videoId) {
        return videoInfoMapper.updateVideoViewNumByVideoId(videoId);
    }

    @Override
    public CoinOperatedInfo findUserIsOperatedCoinByVideoAndUserId(int videoId, Long userId) {
        return videoInfoMapper.selectUserIsOperatedCoinByVideoAndUserId(videoId, userId);
    }

    @Override
    public int findCoinNumByVideoId(long videoId) {
        return videoInfoMapper.selectCoinNumByVideoId(videoId);
    }

    @Override
    public boolean addUserPraiseVideoInfo(int videoId, Long userId, Timestamp timestamp) {
        return videoInfoMapper.insertUserPraiseVideoInfo(videoId, userId, timestamp);
    }

    @Override
    public boolean dropUserPraiseVideoInfo(int videoId, Long userId) {
        return videoInfoMapper.deleteUserPraiseVideoInfo(videoId, userId);
    }

    @Override
    public long findPraiseNumByVideoId(int videoId) {
        return videoInfoMapper.findLikedNumByVideoId(videoId);
    }

    @Override
    public boolean addUserIsNoticeUpInfo(Long followUserId, Long userId, Timestamp timestamp) {
        return videoInfoMapper.insertUserIsNoticeUpInfo(followUserId, userId, timestamp);
    }

    @Override
    public boolean dropUserIsNoticeUpInfo(Long followUserId, Long userId) {
        return videoInfoMapper.deleteUserIsNoticeUpInfo(followUserId, userId);
    }

    @Override
    public List<VideoInfo> findAllVideoInfoByExamineStatusAccessByUserId(Long userId) {
        return videoInfoMapper.selectAllVideoInfoByExamineStatusAccessByUserId(userId);
    }

    /**
     * 查找全部的未审核过并且未通过的记录
     * @param videoInfoPage
     * @return
     */
    @Override
    public IPage<VideoInfo> findAllNotAccessVideoInfo(Page<VideoInfo> videoInfoPage) {
        return videoInfoMapper.selectAllNotAccessVideoInfo(videoInfoPage);
    }

    @Override
    public boolean addCoinOperatedInfo(Long userId, Long videoId) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String createDate = df.format(date);
        return coinOperatedInfoMapper.insertCoinOperatedInfo(userId,videoId,createDate);
    }

    @Override
    public boolean removeCoinOperatedInfo(long userId, long videoId) {
        return coinOperatedInfoMapper.deleteCoinOperatedInfo(userId,videoId);
    }
}
